package com.luoying.itew.util;

import com.luoying.itew.R;
import com.luoying.itew.bean.CollectData;
import com.luoying.itew.bean.CollectorBean;
import com.luoying.itew.bean.MonitorBean;
import com.luoying.itew.bean.UserInfo;
import com.luoying.itew.common.AntaiApplication;
import com.luoying.itew.common.Constant;
import com.luoying.itew.manage.UserManage;

/**
 * Created by lufan on 2018/7/20
 * Class describe 监测点工具类
 */
public class MonitorUtils {

    /**
     * 获取监测点状态
     * @param bean
     * @return
     */
    public static int getMonitorState(MonitorBean bean){
        UserInfo userInfo = UserManage.getInstance().getUserInfo();
        //如果两个数据有空那么直接返回绿色
        if(bean==null||userInfo==null){
            return Constant.WARN_LEVEL_GREEN;
        }
        //监测点报警状态启用 直接返回红色
        if(bean.getMonitorPointStatus()==1){
            return Constant.WARN_LEVEL_RED;
        }
        int userType = userInfo.getUserType();
        if(userType==0){
            return getLevelBySystemUser(bean.getWarnCoefficient());
        } else {
            return getLevelByNonSystemUser(bean.getWarnCoefficient());
        }
    }

    /**
     * 获取采集器预警级别
     * @param bean 采集器对象
     * @return
     */
    public static int CollectorLevel(CollectorBean bean){
        UserInfo userInfo = UserManage.getInstance().getUserInfo();
        //如果两个数据有空那么直接返回绿色
        if(bean==null||userInfo==null){
            return Constant.WARN_LEVEL_GREEN;
        }
        //采集器报警状态启用 直接返回红色
        if(bean.getCollectorStatus()==1){
            return Constant.WARN_LEVEL_RED;
        }
        int userType = userInfo.getUserType();
        if(userType==0){
            return getLevelBySystemUser(bean.getWarnStatus());
        } else {
            return getLevelByNonSystemUser(bean.getWarnStatus());
        }
    }

    /**
     * 预警级别转成对应预警级别
     * @param warnState 预警级别
     * @return
     */
    public static String WarnLevelToString(int warnState){
        String string ;
        switch (warnState){
            default:
            case Constant.WARN_LEVEL_GREEN:
                string = AntaiApplication.context.getString(R.string.warn_leve_green);
                break;
            case Constant.WARN_LEVEL_YELLOW:
                string = AntaiApplication.context.getString(R.string.warn_leve_yellow);
                break;
            case Constant.WARN_LEVEL_ORANGE:
                string = AntaiApplication.context.getString(R.string.warn_leve_orange);
                break;
            case Constant.WARN_LEVEL_RED:
                string = AntaiApplication.context.getString(R.string.warn_leve_red);
                break;
        }
        return string;
    }

    /**
     * 系统用户对应的预警级别
     * @return
     */
    private static int getLevelBySystemUser(int warnCoefficient){
        int level = Constant.WARN_LEVEL_GREEN;
        switch (warnCoefficient){
            case Constant.MONITOR_WARN_COEFFICIENT_GREEN:
                level = Constant.WARN_LEVEL_GREEN;
                break;
            case Constant.MONITOR_WARN_COEFFICIENT_YELLOW:
                level = Constant.WARN_LEVEL_YELLOW;
                break;
            case Constant.MONITOR_WARN_COEFFICIENT_ORANGE:
                level = Constant.WARN_LEVEL_ORANGE;
                break;
            case Constant.MONITOR_WARN_COEFFICIENT_RED:
                level = Constant.WARN_LEVEL_RED;
                break;
        }
        return level;
    }

    /**
     * 非系统用户对应预警级别
     * 报警开关未打情况下 非系统用户只能看到黄色预警
     * @return
     */
    private static int getLevelByNonSystemUser(int warnCoefficient){
        int level = Constant.WARN_LEVEL_GREEN;
        switch (warnCoefficient){
            case Constant.MONITOR_WARN_COEFFICIENT_GREEN:
                level = Constant.WARN_LEVEL_GREEN;
                break;
            case Constant.MONITOR_WARN_COEFFICIENT_YELLOW:
            case Constant.MONITOR_WARN_COEFFICIENT_ORANGE:
            case Constant.MONITOR_WARN_COEFFICIENT_RED:
                level = Constant.WARN_LEVEL_YELLOW;
                break;
        }
        return level;
    }

    /**
     * 数据绑定
     * @param collectorBean
     * @param data
     */
    public static void bindColletrDate(CollectorBean collectorBean, CollectData data){
        int type  = collectorBean.getCollectorType();
        StringBuffer lastData = new StringBuffer();
        switch (type){
            case 5:
                lastData.append("X轴倾斜角").append(data.getCollectParamX()).append(" ")
                        .append("X轴倾斜率").append(data.getCollectDataX()).append(" ")
                        .append("Y轴倾斜角").append(data.getCollectParamY()).append(" ")
                        .append("Y轴倾斜率").append(data.getCollectDataY());
                break;
            case 3:
                lastData.append("温度").append(data.getCollectDataX()).append(" ")
                        .append("湿度").append(data.getCollectDataY());
                break;
            case 0:
                lastData.append("采集数据").append(data.getCollectX()).append(" ")
                        .append(data.getCollectY()).append(" ")
                        .append(data.getCollectZ());
                break;
            case 2:
                lastData.append("水位").append(data.getCollectData());
                break;
            case 8:
                lastData.append("相对沉降").append(data.getCollectDataX()).append(" ")
                        .append("绝对沉降").append(data.getCollectDataY());
                break;
            case 9:
                lastData.append("基准点").append(data.getCollectData());
                break;
            case 10:
                lastData.append("X轴角度").append(data.getCollectDataX()).append(" ")
                        .append("X轴位移").append(data.getCollectParamX()).append(" ")
                        .append("Y轴角度").append(data.getCollectDataY()).append(" ")
                        .append("Y轴位移").append(data.getCollectParamY());
                break;
            default:
                break;
        }
        collectorBean.setLastDate(lastData.toString());
    }


}
